package com.huiquan.slang.controller;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.huiquan.analysis.domain.User;
import com.huiquan.framework.base.ReturnData;
import com.huiquan.framework.utils.RequestParamUtil;
import com.huiquan.slang.service.SlangSentenceService;

@Controller
public class SlangSentenceController {

	@Autowired
	private SlangSentenceService slangSentenceService;

	@SuppressWarnings("unchecked")
	@RequestMapping(value = "/slang/index.htm")
	public ModelAndView index(HttpServletRequest req, HttpServletResponse resp) throws IOException {
		User user = (User) req.getSession().getAttribute("user");
		String tabid = req.getParameter("tabid");

		// 维护页面参数到session
		Map<String, Object> pageParam;
		if (tabid != null && !tabid.isEmpty()) {
			pageParam = new HashMap<>();
			pageParam.put("tabid", tabid);
			pageParam.put("start", "1");
			pageParam.put("exactSearchFlag", "0");

			req.getSession().setAttribute("pageParam", pageParam);
		} else {
			pageParam = (Map<String, Object>) req.getSession().getAttribute("pageParam");
		}
		if (user.getRole() == 8) {
			return currentPackage(req, resp);
		} else {
			return sentenceList(req, resp);
		}
	}

	@SuppressWarnings("unchecked")
	@RequestMapping(value = "/slang/sentence_list.htm")
	public ModelAndView sentenceList(HttpServletRequest req, HttpServletResponse resp) throws IOException {
		String tabid = req.getParameter("tabid");

		// 维护页面参数到session
		Map<String, Object> pageParam;
		if (tabid != null && !tabid.isEmpty()) {
			pageParam = new HashMap<>();
			pageParam.put("tabid", tabid);
			pageParam.put("start", "1");
			pageParam.put("exactSearchFlag", "0");

			req.getSession().setAttribute("pageParam", pageParam);
		} else {
			pageParam = (Map<String, Object>) req.getSession().getAttribute("pageParam");
		}
		String start = RequestParamUtil.getParamByRequestOrSession(req, pageParam, "start");
		String keyword = RequestParamUtil.getParamByRequestOrSession(req, pageParam, "keyword");
		String keyUname = RequestParamUtil.getParamByRequestOrSession(req, pageParam, "keyUname");
		String property = RequestParamUtil.getParamByRequestOrSession(req, pageParam, "propertyKey");
		if (StringUtils.isBlank(tabid)) {
			tabid = (String) pageParam.get("tabid");
		}
		User user = (User) req.getSession().getAttribute("user");

		if (property != null) {
			property = property.trim();
		}

		String oppositePropertyFlag = RequestParamUtil.getParamByRequestOrSession(req, pageParam,
				"oppositePropertyFlag");
		if (oppositePropertyFlag == null || oppositePropertyFlag.trim().isEmpty()) {
			oppositePropertyFlag = "0";
		}

		return slangSentenceService.getList(Integer.parseInt(start), keyword, keyUname, user, property,
				oppositePropertyFlag, tabid);
	}

	@RequestMapping(value = "/slang/sentence_pre_edit.htm")
	public ModelAndView preEdit(HttpServletRequest req, HttpServletResponse resp) throws IOException {
		// 维护页面参数到session
		@SuppressWarnings("unchecked")
		Map<String, Object> pageParam = (Map<String, Object>) req.getSession().getAttribute("pageParam");
		String start = RequestParamUtil.getParamByRequestOrSession(req, pageParam, "start");
		String keywordStr = RequestParamUtil.getParamByRequestOrSession(req, pageParam, "keyword");
		String keyUname = RequestParamUtil.getParamByRequestOrSession(req, pageParam, "keyUname");
		String propertyKey = RequestParamUtil.getParamByRequestOrSession(req, pageParam, "propertyKey");
		String oppositePropertyFlag = RequestParamUtil.getParamByRequestOrSession(req, pageParam,
				"oppositePropertyFlag");
		Long id = Long.parseLong(RequestParamUtil.getParamByRequestOrSession(req, pageParam, "sid"));
		User user = (User) req.getSession().getAttribute("user");

		return slangSentenceService.preEdit(Integer.parseInt(start), keywordStr, keyUname, id, user, propertyKey,
				oppositePropertyFlag);
	}

	/**
	 * 获取当前用户的包
	 * 
	 * @param req
	 * @param resp
	 * @return
	 * @throws IOException
	 */
	@SuppressWarnings("unchecked")
	@RequestMapping(value = "/slang/current_package.htm")
	public ModelAndView currentPackage(HttpServletRequest req, HttpServletResponse resp) throws IOException {
		User user = (User) req.getSession().getAttribute("user");
		Map<String, Object> pageParam = (Map<String, Object>) req.getSession().getAttribute("pageParam");
		pageParam.put("tabid", "28_7");
		return slangSentenceService.currentPackage(user);
	}

	@SuppressWarnings("unchecked")
	@ResponseBody
	@RequestMapping(value = "/slang/sentence_edit.do")
	public ReturnData edit(HttpServletRequest req, HttpServletResponse resp) throws IOException {
		User user = (User) req.getSession().getAttribute("user");

		Map<String, Object> map = req.getParameterMap();

		Long id = Long.parseLong(((String[]) map.get("id"))[0]);
		String[] vocabularyStrs = (String[]) map.get("vocabulary");
		String[] characterStrs = (String[]) map.get("character");
		String[] propertyStrs = (String[]) map.get("property");
		return slangSentenceService.edit(id, vocabularyStrs, characterStrs, propertyStrs, user);
	}

	@SuppressWarnings("unchecked")
	@ResponseBody
	@RequestMapping(value = "/slang/sentence_preview.do")
	public ReturnData preview(HttpServletRequest req, HttpServletResponse resp) throws Exception {
		Map<String, Object> map = req.getParameterMap();

		Long id = Long.parseLong(((String[]) map.get("id"))[0]);
		String[] vocabularyStrs = (String[]) map.get("vocabulary");
		String[] characterStrs = (String[]) map.get("character");
		String[] propertyStrs = (String[]) map.get("property");
		return slangSentenceService.preview(id, vocabularyStrs, characterStrs, propertyStrs);
	}

	@ResponseBody
	@RequestMapping(value = "/slang/sentence_submit_package.do")
	public ReturnData submitPackage(HttpServletRequest req, HttpServletResponse resp) throws Exception {
		User user = (User) req.getSession().getAttribute("user");

		return slangSentenceService.submitPackage(user);
	}

	@ResponseBody
	@RequestMapping(value = "/slang/sentence_delete.do")
	public ReturnData delete(HttpServletRequest req, HttpServletResponse resp) throws Exception {
		long id = Long.parseLong(req.getParameter("id"));
		User user = (User) req.getSession().getAttribute("user");

		return slangSentenceService.delete(id, user);
	}

	@ResponseBody
	@RequestMapping(value = "/slang/sentence_restore.do")
	public ReturnData restore(HttpServletRequest req, HttpServletResponse resp) throws Exception {
		long id = Long.parseLong(req.getParameter("id"));
		User user = (User) req.getSession().getAttribute("user");

		return slangSentenceService.restore(id, user);
	}
}
